import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { AfMessageService, AfNavigationService } from '@core';
import { STColumn, STColumnTag, STColumnBadge, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';

import { SecurityService } from '../security.service';

@Component({
  selector: 'app-security-user',
  templateUrl: './user.component.html'
})
export class SecurityUserComponent implements OnInit {
  rows: any[] = [];
  queryParams: any = { username: '', nickName: '' };
  url = '';
  req = { params: this.queryParams, lazyLoad: false };

  isQuery = false;

  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: '账号', index: 'username' },
    { title: '用户名', index: 'nickName' },
    { title: '手机号', index: 'phone' },
    // { title: '员工编号', index: 'employeeId' },
    // { title: '邮箱地址', index: 'email' },
    { title: '更新人', index: 'lastModifiedBy' },
    { title: '更新日期', type: 'date', index: 'lastModifiedDate' },
    {
      title: '锁定状态',
      type: 'tag',
      index: 'accountNonLocked',
      tag: {
        true: { text: '正常', color: 'success' },
        false: { text: '锁定', color: 'orange' }
      }
    },
    {
      title: '状态',
      type: 'tag',
      index: 'disabled',
      tag: {
        true: { text: '禁用', color: 'cyan' },
        false: { text: '启用', color: 'success' }
      }
    },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          tooltip: `修改账号信息`,
          click: record => {
            this.editNavigate(record, 0);
          }
        },
        {
          text: '分配角色',
          icon: 'safety',
          tooltip: `分配账号信息`,
          click: record => {
            this.editNavigate(record, 1);
          }
        },
        {
          text: '数据权限',
          icon: 'safety',
          tooltip: `配置数据权限`,
          click: record => {
            this.editNavigate(record, 2);
          }
        }
      ]
    }
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private cdr: ChangeDetectorRef,
    private msgSrv: AfMessageService,
    private navSrv: AfNavigationService,
    private securitySrv: SecurityService
  ) {}

  ngOnInit(): void {
    this.url = this.securitySrv.queryUserUrl();
  }

  addNavigate(): void {
    this.navSrv.navigateByUrl('/security/user/edit?btn=add');
  }

  editNavigate(record: any, tagIndex: any) {
    this.navSrv.navigateByUrl(`/security/user/edit?btn=edit&record=${JSON.stringify(record)}&index=${tagIndex}`);
  }

  query(event: any = undefined) {
    // if (!this.queryParams.templeteType || this.queryParams.templeteType === '') {
    //   this.msgSrv.msg_warning(`请选择模版类型`);
    //   return;
    // }
    this.st.reset(this.queryParams);
  }
}
